const express = require('express');

const  router = express.Router();

const Book = require("../model/addbook")
const Kind = require("../model/addkind")



// console.log(data);


// 添加书籍
router.post('/add', async (req,res)=>{
    const { name,cover,wirter,type ,dec,recommended,general,no,readnum,wrodnum,humnum} = req.body;

    // wxNickName,wxHeadPhoto,wxOpenId 这三个值都是从wxInfo
    
    // 数据过滤
    if(!name||!cover||!wirter||!type||!dec||!recommended||!general||!no||!readnum||!wrodnum||!humnum) return res.send( {success:false,info:'请填写必要参数'});
   

    // 添加入库
    try{
        const one = await Book.findOne({ name }) // 找不到的话返回 null 
        if(one) return res.send({ success:false,info:'当前书已存在' })

        await Book.create({
            name,
            cover,
            wirter,
            type,
            dec,
            recommended,
            general,
            no,
            readnum,
            wrodnum,
            humnum,
        })
        res.send({success:true,info:'添加成功'})

    } catch(e) {
        res.send( {success:false,info:'未知错误 请于网站管理员联系'});
    }


})

// 获取全部的书籍

router.post('/getAllBook', async (req,res)=>{
    let { page,limit,wirter,id } = req.body;
     page = page || 1; // 当前第几页
    
     limit = limit || 3; // 单页返回的条数限制
     console.log(req.body,'body');
      // 初始化 查询条件      
      let where = {  }
      // 按名字查找
      if(wirter )  where.wirter = wirter
      //按类型查
      if(id )  where._id = id
      console.log(where,'条件');

    
      const skip =  (page - 1 ) * limit; // 查询的起点（偏移量）
      try {
        let Books = await Book.find(where,{},{skip,limit}) // 分页查询
        let wrt= await Book.find(where,{}) 
        let count = await Book.count(where) // 获取符合条件的总数

        id||wirter? res.send({success:true,info:'查询成功',data:wrt,count}):res.send({success:true,info:'查询成功',data:Books,count});
        // res.send({success:true,info:'查询成功',data:Books,count});
      }catch(e){
          console.log(e)
        res.send({success:false,info:'获取失败'})
      }

})


//按作者名字查询

router.post('/getwriteBook', async (req,res)=>{
  let {wirter } = req.body;
 
  console.log( req.body,'wirter');
  
   console.log(req.body,'body');
    // 初始化 查询条件      
    let where = {  }
   
   
    if(!wirter) return
    if(wirter )  where.wirter = wirter

    console.log(where,'条件');

  

    try {
     
      let wrt= await Book.find(where,{}) 
      let count = await Book.count(where) // 获取符合条件的总数
      // wirter? res.send({success:true,info:'查询成功',data:wrt,count}):res.send({success:true,info:'查询成功',data:Books,count});
      res.send({success:true,info:'查询成功',data:wrt,count});
    }catch(e){
        console.log(e)
      res.send({success:false,info:'获取失败'})
    }

})



//按类型查找书籍

router.post('/gettypeBook', async (req,res)=>{
  let {type } = req.body;
 
  
  
   console.log(req.body,'body');
    // 初始化 查询条件      
    let where = {  }
   
   
    //按类型查
    if(type )  where.type = type
    console.log(where,'条件');

  

    try {
     
      let wrt= await Book.find(where,{}) 
      let count = await Book.count(where) // 获取符合条件的总数
      // wirter? res.send({success:true,info:'查询成功',data:wrt,count}):res.send({success:true,info:'查询成功',data:Books,count});
      res.send({success:true,info:'查询成功',data:wrt,count});
    }catch(e){
        console.log(e)
      res.send({success:false,info:'获取失败'})
    }

})




// 详情页获取单个书籍信息

router.post('/bookdetail',async (req,res)=>{

    const { id } = req.body;
    if(!id)  res.send({success:false,info:'请一定要传入id'})

    try {
        let book = await Book.findById(id) // 分页查询
      
        res.send({success:true,info:'查询成功',data:book});
      }catch(e){
         
        res.send({success:false,info:'获取失败'})
      }

})




// 添加书籍种类接口
router.post('/abc', async (req,res)=>{
  const {cover,type ,dec } = req.body;

  // wxNickName,wxHeadPhoto,wxOpenId 这三个值都是从wxInfo
  
  // 数据过滤
  if(!cover||!type||!dec) return res.send( {success:false,info:'请填写必要参数'});
 

  // 添加入库
  try{
      const one = await Kind.findOne({ type }) // 找不到的话返回 null 
      console.log(one);
      if(one) return res.send({ success:false,info:'当前书已存在' ,a:one})

      await Kind.create({
        
          cover,
          
          type,
          dec,
      })
      res.send({success:true,info:'添加成功'})

  } catch(e) {
    
      res.send( {success:false,info:'未知错误 请于网站管理员联系'});
  }


})


//获取全部书籍种类接口
router.post('/getallkind', async (req,res)=>{
  let { page,limit,type } = req.body;
   page = page || 1; // 当前第几页
   limit = limit || 100; // 单页返回的条数限制

    // 初始化 查询条件      
    let where = {  }
    // 按名字查找
    if(type )  where.type = type
  
  
    const skip =  (page - 1 ) * limit; // 查询的起点（偏移量）
    try {
      let Kinds = await Kind.find(where,{},{skip,limit}) // 分页查询
      let count = await Kind.count(where) // 获取符合条件的总数
      res.send({success:true,info:'查询成功',data:Kinds,count});
    }catch(e){
        console.log(e)
      res.send({success:false,info:'获取失败'})
    }

})





module.exports = router;